/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.manual.service;

import com.fzhucc.modules.manual.domain.RaceManual;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.manual.repository.RaceManualRepository;
import com.fzhucc.modules.manual.domain.dto.RaceManualDto;
import com.fzhucc.modules.manual.domain.dto.RaceManualQueryCriteria;
import com.fzhucc.modules.manual.service.mapstruct.RaceManualMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2023-08-16
**/
@Service
@RequiredArgsConstructor
public class RaceManualService {

    private final RaceManualRepository raceManualRepository;
    private final RaceManualMapper raceManualMapper;

    public Map<String,Object> queryAll(RaceManualQueryCriteria criteria, Pageable pageable){
        Page<RaceManual> page = raceManualRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(raceManualMapper::toDto));
    }

    public List<RaceManualDto> queryAll(RaceManualQueryCriteria criteria){
        return raceManualMapper.toDto(raceManualRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public RaceManualDto findById(Integer id) {
        RaceManual raceManual = raceManualRepository.findById(id).orElseGet(RaceManual::new);
        ValidationUtil.isNull(raceManual.getId(),"RaceManual","id",id);
        return raceManualMapper.toDto(raceManual);
    }

    @Transactional(rollbackFor = Exception.class)
    public RaceManualDto create(RaceManual resources) {
        return raceManualMapper.toDto(raceManualRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(RaceManual resources) {
        RaceManual raceManual = raceManualRepository.findById(resources.getId()).orElseGet(RaceManual::new);
        ValidationUtil.isNull( raceManual.getId(),"RaceManual","id",resources.getId());
        raceManual.copy(resources);
        raceManualRepository.save(raceManual);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            raceManualRepository.deleteById(id);
        }
    }

    public void download(List<RaceManualDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RaceManualDto raceManual : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("标题", raceManual.getTitle());
            map.put("小标题", raceManual.getSmallTitle());
            map.put("来源 字典race_source", raceManual.getSource());
            map.put("内容  ", raceManual.getContext());
            map.put("内容分类  字典 race_context_type", raceManual.getContextType());
            map.put("排序", raceManual.getSort());
            map.put("是否显示", raceManual.getIsShow());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}